//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LevenbergMarquardt.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Optimization/Problem.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (Microsoft::FSharp::Core::FSharpOption<Double>^ epsfcn, Microsoft::FSharp::Core::FSharpOption<Double>^ xtol, Microsoft::FSharp::Core::FSharpOption<Double>^ gtol) : COptimizationMethod(CLevenbergMarquardt::typeid)
{
    try
    {
#ifdef HANDLE
        _phLevenbergMarquardt = NULL;
#endif
        QuantLib::Real _epsfcn = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (epsfcn) ? (QuantLib::Real)ValueHelper::Convert (epsfcn->Value) : 1.0e-8); //4
        QuantLib::Real _xtol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (xtol) ? (QuantLib::Real)ValueHelper::Convert (xtol->Value) : 1.0e-8); //4
        QuantLib::Real _gtol = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (gtol) ? (QuantLib::Real)ValueHelper::Convert (gtol->Value) : 1.0e-8); //4
        _ppLevenbergMarquardt = new boost::shared_ptr<QuantLib::LevenbergMarquardt> (new QuantLib::LevenbergMarquardt ( _epsfcn,  _xtol,  _gtol ));
        SetOptimizationMethod (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (boost::shared_ptr<QuantLib::LevenbergMarquardt>& childNative, Object^ owner) : COptimizationMethod(CLevenbergMarquardt::typeid)
{
#ifdef HANDLE
	_phLevenbergMarquardt = NULL;
#endif
	_ppLevenbergMarquardt = &childNative;
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
}
Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (QuantLib::LevenbergMarquardt& childNative, Object^ owner) : COptimizationMethod(CLevenbergMarquardt::typeid)
{
#ifdef HANDLE
	_phLevenbergMarquardt = NULL;
#endif
	_ppLevenbergMarquardt = new boost::shared_ptr<QuantLib::LevenbergMarquardt> (&childNative);
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
    _LevenbergMarquardtOwner = owner;
    _OptimizationMethodOwner = owner;
}

Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (CLevenbergMarquardt^ copy) : COptimizationMethod(CLevenbergMarquardt::typeid)
{
#ifdef HANDLE
	_phLevenbergMarquardt = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLevenbergMarquardt = new boost::shared_ptr<QuantLib::LevenbergMarquardt> (copy->GetShared());
        _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
    }
}
Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (System::Type^ t) : COptimizationMethod(CLevenbergMarquardt::typeid)
{
#ifdef HANDLE
	_phLevenbergMarquardt = NULL;
#endif
	if (!t->IsSubclassOf(CLevenbergMarquardt::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (QuantLib::Handle<QuantLib::LevenbergMarquardt>& childNative, Object^ owner)  : COptimizationMethod(CLevenbergMarquardt::typeid)
{
	_phLevenbergMarquardt = &childNative;
	_ppLevenbergMarquardt = &static_cast<boost::shared_ptr<QuantLib::LevenbergMarquardt>>(childNative.currentLink());
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
    _LevenbergMarquardtOwner = owner;
}
Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (QuantLib::Handle<QuantLib::LevenbergMarquardt> childNative)  : COptimizationMethod(CLevenbergMarquardt::typeid)
{
	_phLevenbergMarquardt = &childNative;
	_ppLevenbergMarquardt = &static_cast<boost::shared_ptr<QuantLib::LevenbergMarquardt>>(childNative.currentLink());
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CLevenbergMarquardt::CLevenbergMarquardt (QuantLib::LevenbergMarquardt childNative)  : COptimizationMethod(CLevenbergMarquardt::typeid)
{
#ifdef HANDLE
	_phLevenbergMarquardt = NULL;
#endif
	_ppLevenbergMarquardt = new boost::shared_ptr<QuantLib::LevenbergMarquardt> (new QuantLib::LevenbergMarquardt (childNative));
    _ppOptimizationMethod = new boost::shared_ptr<QuantLib::OptimizationMethod> (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
}
#endif

Cephei::QL::Math::Optimization::CLevenbergMarquardt::~CLevenbergMarquardt ()
{
    if (_ppLevenbergMarquardt != NULL)
    {
	    delete _ppLevenbergMarquardt;
        _ppLevenbergMarquardt = NULL;
    }
}
Cephei::QL::Math::Optimization::CLevenbergMarquardt::!CLevenbergMarquardt ()
{
    if (_ppLevenbergMarquardt != NULL)
    {
	    delete _ppLevenbergMarquardt;
    }
}
QuantLib::LevenbergMarquardt& Cephei::QL::Math::Optimization::CLevenbergMarquardt::GetReference ()
{
    if (_ppLevenbergMarquardt == NULL) throw gcnew NativeNullException ();
	return **_ppLevenbergMarquardt;
}
boost::shared_ptr<QuantLib::LevenbergMarquardt>& Cephei::QL::Math::Optimization::CLevenbergMarquardt::GetShared ()
{
    if (_ppLevenbergMarquardt == NULL) throw gcnew NativeNullException ();
	return *_ppLevenbergMarquardt;
}
QuantLib::LevenbergMarquardt* Cephei::QL::Math::Optimization::CLevenbergMarquardt::GetPointer ()
{
    if (_ppLevenbergMarquardt == NULL) throw gcnew NativeNullException ();
	return &**_ppLevenbergMarquardt;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LevenbergMarquardt>& Cephei::QL::Math::Optimization::CLevenbergMarquardt::GetHandle ()
{
	if (_phLevenbergMarquardt == NULL)
	{
		_phLevenbergMarquardt = new Handle<QuantLib::LevenbergMarquardt> (*_ppLevenbergMarquardt);
	}
	return *_phLevenbergMarquardt;
}
#endif
bool Cephei::QL::Math::Optimization::CLevenbergMarquardt::HasNative () 
{
	return (_ppLevenbergMarquardt != NULL);
}

Int32 Cephei::QL::Math::Optimization::CLevenbergMarquardt::GetInfo::get ()
{
    try
    {
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppLevenbergMarquardt)->getInfo ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Math::Optimization::EndCriteria::TypeEnum Cephei::QL::Math::Optimization::CLevenbergMarquardt::Minimize (Cephei::QL::Math::Optimization::IProblem^ P, Cephei::QL::Math::Optimization::IEndCriteria^ endCriteria)
{
    CProblem^ _CP;
    CEndCriteria^ _CendCriteria;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _CP = safe_cast<CProblem^> (P);
        _CP->Lock();
        QuantLib::Problem& _P = static_cast<QuantLib::Problem&> (_CP->GetReference ()); 
        _CendCriteria = safe_cast<CEndCriteria^> (endCriteria);
        _CendCriteria->Lock();
        QuantLib::EndCriteria& _endCriteria = static_cast<QuantLib::EndCriteria&> (_CendCriteria->GetReference ()); 
    	QuantLib::EndCriteria::Type _rv = (QuantLib::EndCriteria::Type)(*_ppLevenbergMarquardt)->minimize ( _P,  _endCriteria );   
        QL::Math::Optimization::EndCriteria::TypeEnum _nrv = (QL::Math::Optimization::EndCriteria::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CP != nullptr) _CP->Unlock();
        if (_CendCriteria != nullptr) _CendCriteria->Unlock();
    }
}
Cephei::QL::Math::Optimization::ILevenbergMarquardt^ Cephei::QL::Math::Optimization::CLevenbergMarquardt::Fcn (Int32 m, Int32 n, Double x, Double fvec, Int32 iflag)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        int _m = (int)ValueHelper::Convert (m);
        int _n = (int)ValueHelper::Convert (n);
        double* _x = (double*)&x;
        double* _fvec = (double*)&fvec;
        int* _iflag = (int*)ValueHelper::Convert (iflag);
    	(*_ppLevenbergMarquardt)->fcn ( _m,  _n,  _x,  _fvec,  _iflag );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::ILevenbergMarquardt^ Cephei::QL::Math::Optimization::CLevenbergMarquardt_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ epsfcn, Microsoft::FSharp::Core::FSharpOption<Double>^ xtol, Microsoft::FSharp::Core::FSharpOption<Double>^ gtol)
{
    return gcnew CLevenbergMarquardt ( epsfcn,  xtol,  gtol);
}
